//https://leetcode.cn/problems/valid-triangle-number/submissions/549646557/

class Solution {
public:
    int triangleNumber(vector<int>& nums) {

        //排序
        sort(nums.begin(), nums.end());

        //统计
        int n = nums.size();
        int sum = 0;
        for (size_t i = n - 1; i >= 2; i--)
        {
            int left = 0, right = i - 1;
            while (left < right)
            {
                if (nums[right] + nums[left] > nums[i])
                {
                    sum += right - left;
                    right--;
                }
                else
                {
                    left++;
                }
            }
        }
        return sum;

    }
};